package hng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hng.dto.AddCommentDTO;
import hng.dto.ProductCommentDTO;
import hng.exception.BizException;
import hng.interceptor.LoginInterceptor;
import hng.mapper.OrdersMapper;
import hng.model.*;
import hng.mapper.ProductCommentMapper;
import hng.service.OrdersService;
import hng.service.ProductCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hng.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品评论表 服务实现类
 * </p>
 *
 * @author 告白
 * @since 2024-06-27
 */
@Service
public class ProductCommentServiceImpl extends ServiceImpl<ProductCommentMapper, ProductCommentDO> implements ProductCommentService {
    @Autowired(required = false)
    private ProductCommentMapper productCommentMapper;

    @Autowired(required = false)
    private ProductCommentService productCommentService;

    @Autowired
    private ProductService productService;

    @Autowired
    private OrdersService orderService;

    @Autowired(required = false)
    private OrdersMapper ordersMapper;

    private static final Integer ORDER_STATUS_COMPLETED = 4;//设置订单状态常量为5，完成订单状态
    /**
     * 添加评论
     * **/
    @Override
    public void add(AddCommentDTO addCommentDTO){
        LoginUser loginUser = LoginInterceptor.threadLocal.get(); //从拦截器获取当前登录用户

        // 评分
        Integer rating = addCommentDTO.getRating();
        // 评论内容
        String content = addCommentDTO.getContent();
        List<ProductCommentDO> list = new ArrayList<>();
        for (ProductCommentDTO orderItem : addCommentDTO.getOrderItems()) {
            ProductCommentDO productCommentDO = new ProductCommentDO();
            BeanUtils.copyProperties(orderItem, productCommentDO);
            productCommentDO.setRating(rating);
            productCommentDO.setUserId(loginUser.getId());
            productCommentDO.setContent(content);
            list.add(productCommentDO);
        }
        productCommentService.saveBatch(list);

        // 获取订单id
        Integer orderId = list.get(0).getOrderId();
        // 设置订单状态 已评论
        OrdersDO ordersDO = new OrdersDO();
        ordersDO.setId(orderId);
        ordersDO.setOrderStatus(5);
        ordersMapper.updateById(ordersDO);
    }
    /**
     * 删除评论
     * **/
    @Override
    public void del(Integer id) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();//从拦截器获取当前登录用户
        int row = productCommentMapper.delete(new LambdaQueryWrapper<ProductCommentDO>()
//                用Mapper继承来的Delect方法，在方法里新建一个条件构造器，构造搜索的条件
                .eq(ProductCommentDO::getId,id) //条件构造器.eq表示等于判断，当商品评论的id等于传入id
                .eq(ProductCommentDO::getUserId,loginUser.getId())//同时UserID为当前登录id时才可以删除
        );

    }
    /**
     * 修改评论
     * **/
    @Override
    public void update(ProductCommentDTO productCommentDTO) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();//获取用户信息
        ProductCommentDO productCommentDO = new ProductCommentDO();//创建DO实例
        BeanUtils.copyProperties(productCommentDTO, productCommentDO);//把DTO数据传给DO
        productCommentDO.setUserId(loginUser.getId());//从用户信息获取用户id给DO
        int row = productCommentMapper.update(productCommentDO,new LambdaQueryWrapper<ProductCommentDO>()
                .eq(ProductCommentDO::getUserId ,loginUser.getId())//确定当前删除评论的用户ID为当前登录ID
                .eq(ProductCommentDO::getId,productCommentDTO.getId())//确保DTO中的id 为DO中的id
        );
    }
    /**
     * 查看评论
     * **/
    @Override
    public List<ProductCommentDO> productCommentList() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();//获取用户信息
        List<ProductCommentDO> productCommentDOList = productCommentMapper.selectList(new LambdaQueryWrapper<ProductCommentDO>()
//                判断用户是否查找商品评论列表
                .eq(ProductCommentDO::getUserId,loginUser.getId())
                        .orderByDesc(ProductCommentDO::getUserId)
        );
        return productCommentDOList;
    }


}
